//场外势力指令生成，主要是空军，伞降兵等的操作及生成指令

var courtDirectionMenu = {
	selectTroopSymbol : null,
	
	//route Menus
	routeMenus : [],
	
	Initialize : function(){
		var hDiv = 0.5 * tileEngine.tileHeight;
		var wDiv = 0.5 * tileEngine.tileWidth;
		this.routeMenuLocation = {x : -2 * wDiv, y : 0.75 * hDiv};
		
		this.selectTroopSymbol = new cc.Sprite(resource.selectsymbol_png);
		this.selectTroopSymbol.attr({x: -5000, y: -5000, color: cc.color(255, 255, 64)});
		this.selectTroopSymbol.runAction(cc.repeatForever(cc.sequence(cc.fadeIn(0.35),cc.delayTime(0.8),cc.fadeOut(0.35))));
		courtPanel.node.addChild(this.selectTroopSymbol, 50);
		
		this.routeMenus = [];
		this.routeMenus[0] =	this._CreateBlinkMenu(cc.LabelTTF.create("完毕", "simhei", 28),	0.0, CompleteMenuClick);
		this.routeMenus[1] =	this._CreateBlinkMenu(cc.LabelTTF.create("重设", "simhei", 28),	0.0, ResetMenuClick);
		
		var menu = cc.Menu.create(this.routeMenus[0], this.routeMenus[1]);
		menu.attr({x : 0, y : 0});
		courtPanel.node.addChild(menu, 112);
	},
	
	//0 <= blinkOffset < 1
	_CreateBlinkMenu : function(symbolNode, blinkOffset, callback){
		var button = new cc.MenuItemImage(resource.troopBtnNormal_png, resource.troopBtnDown_png, resource.troopBtnNormal_png, callback, this);
		button.attr({x: -5000, y: -5000});

		symbolNode.attr({x: button.width * 0.5, y: button.height * 0.5});
		button.addChild(symbolNode);

		button.runAction(cc.repeatForever(cc.sequence(
				cc.delayTime(blinkOffset),
				cc.fadeOut(0.25),
				cc.fadeIn(0.25),
				cc.delayTime(1.0 - blinkOffset)
		)));
		return button;
	},

	SetSelectTroopSymbol : function(coord){
		this.selectTroopSymbol.setPosition(coord.x, coord.y);
	},

	HideSelectTroopSymbol : function(){
		this.selectTroopSymbol.setPosition(-5000, -5000);
	},

	SwitchRouteMenu : function(tileIndex, isCompleteMenu){
		var showid = isCompleteMenu ? 0 : 1;
		var hideid = 1 - showid;
		var coord = courtPanel.IndexToCoord(tileIndex);
		this.routeMenus[hideid].setPosition(-5000, -5000);
		this.routeMenus[showid].setPosition(coord.x + this.routeMenuLocation.x, coord.y + this.routeMenuLocation.y);
	},

	HideRouteMenu : function(){
		this.routeMenus[0].setPosition(-5000, -5000);
		this.routeMenus[1].setPosition(-5000, -5000);
	},
	
	SetAllMenuEnable : function(enable){
		this.routeMenus[0].enabled = enable;
		this.routeMenus[1].enabled = enable;
	},
};

var OnCourtPanelSelectTroop = function(location){
	var tileIndex = courtPanel.CoordToIndex(location);
	
	var foundTroop = courtTroop.GetTileTroop(tileIndex, false);
	if (foundTroop != -1){
		tipPanel.ShowTroopParam(foundTroop);

		if (campaign.troop[foundTroop].force != showingForce){
			//选中敌对单位
			return;
		}
		directionMenu.HideSelectTroopSymbol();
		directionMenu.HideRouteMenu();

		directionMenu.HideSelectSymbol();
		directionInfo.BlinkTroopRoute(foundTroop);
		if (foundTroop == selectedTroop){
			//再次选中已选单位
			return;
		}
		//选中新单位
		oldTroop = selectedTroop;
		selectedTroop = foundTroop;

		directionMenu.HideRouteRange();

		var troopCoord = {x:courtTroop.troopHolders[selectedTroop].x,y:courtTroop.troopHolders[selectedTroop].y};
		directionMenu.HideSelectTroopSymbol();
		courtDirectionMenu.SetSelectTroopSymbol(troopCoord);
		
		//播放standby音效
		effectEngine.PlayOnNext(campaign.troop[selectedTroop].standby_effect, false, 1);

		if (oldTroop >= 0 && campaign.troop[oldTroop].court_force == false){
			directionMenu.HideDirectionMenu(oldTroop);
		}
		
		//以下为允许命令发布
		if (manipulateEnable == false)
			return;

		if (direction[selectedTroop].arranged){//已有了安排，则可以重设
			selectMapStatus = 0;
			courtDirectionMenu.SwitchRouteMenu(tileIndex, false);
		}else{
			//无安排，则必须安排
			selectMapStatus = 1;
			courtDirectionMenu.SwitchRouteMenu(tileIndex, true);
		}
	}else{
		//选中空白
	}
};

var OnMainMapSelectCourtRoute = function(location){
	//选择新的路径点
	var tileIndex = tileEngine.CoordToIndex(location);
	var coord = tileEngine.IndexToCoord(tileIndex);

	directionMenu.SetSelectSymbol(coord);

	var tgtRouteTiles = direction[selectedTroop].routeTiles;

	if (tgtRouteTiles[tgtRouteTiles.length - 1] == tileIndex){
		//选中的路径点即当前路径点
	}else{
		//计算路径
		selectMapStatus = 2;
		//设置按钮位置
		courtDirectionMenu.HideRouteMenu();
		directionMenu.SwitchRouteMenu(tileIndex, true);
		
		direction[selectedTroop].routeTiles[1] = tileIndex;
		direction[selectedTroop].attackIdxOfRoute[1] = 1;

		//绘制进军路径
		if (direction[selectedTroop].routeTiles.length > 1){
			directionInfo.DrawTroopCourtRoute(selectedTroop, direction[selectedTroop].routeTiles, direction[selectedTroop].attackIdxOfRoute);
		}
	}
};